﻿ /*
 * ---------------------------------------------------
 * 作者: MTQ 
 * 日期： 2021/5/6 12:46:31 
 * ---------------------------------------------------
 */
    using System;                                    
    using System.Collections.Generic;                
    using System.Data;                               
    using System.Data.Entity;                        
    using System.Linq;                               
    using System.Web;                                
    using System.Web.Mvc;                            
    using MTQ.Model;             
    using MTQ.BLL;             
    using MTQ.IBLL;             
    using MTQ.Common;             
    using System.IO;                                                   
                                                       
                                                       
                                                       
                                                       
    namespace MTQ.Web.Controllers                    
    {                                                
                                                     
        public class M_EmailNoticeController : BaseController 
        {                                            
                                                     
         private IM_EmailNoticeService  im_emailnoticeService = new M_EmailNoticeService();          
                                                     
                                                     
          //                                           
          // GET: //         
          [AccountAuth(Roles = "M_EmailNotice_Index")]  
          public ActionResult Index()      
          {                                          
                 return View();  
           }                              
                                                     
                                                     
          [AccountAuth(Roles = "M_EmailNotice_Index")]      
           public ActionResult Search()           
           {                                          
                                                     
                                                     
              ServertLayuiTable<M_EmailNotice> servertBootStrapTable = new ServertLayuiTable<M_EmailNotice>();     
                GetSearchData(ref servertBootStrapTable, true);                                      
                  
                                                     
               return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);   
           }                                          
                                                      
           private void GetSearchData(ref ServertLayuiTable<M_EmailNotice> servertBootStrapTable, bool page)                                            
          {                                            
                M_EmailNoticeQuery im_emailnoticeQuery = new M_EmailNoticeQuery();                                      
                im_emailnoticeQuery.Searchkey = GetRequestQueryString("keyword");                                             
                                                      
               im_emailnoticeQuery.UserControlProjectNumber = UserControlProjectNumber;                                       
               if (page)                                       
               {                                       
                     servertBootStrapTable = im_emailnoticeService.LoadLayuiSearchData(im_emailnoticeQuery, Request);                                  
                }                                      
                else                                      
                {                                      
                      IQueryable<M_EmailNotice>  im_emailnotice = im_emailnoticeService.GetIQueryable(im_emailnoticeQuery);                                
                      servertBootStrapTable.data = im_emailnotice .ToList();                                
                       servertBootStrapTable.count = im_emailnotice .Count();                                
                }                                      
           }                                           
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
                                                      
            //          
            // GET: /M_EmailNotice/Create          
            [AccountAuth(Roles = "M_EmailNotice_Create")]          
            public ActionResult Create()          
            {          
                M_EmailNotice m_emailnotice = new M_EmailNotice();     
                m_emailnotice.EN_ID = System.Guid.NewGuid();     
                return View(m_emailnotice);          
            }          
                     
            //          
            // POST: /M_EmailNotice/Create          
            [HttpPost]          
            [AccountAuth(Roles = "M_EmailNotice_Create")]          
            public ActionResult Create(M_EmailNotice m_emailnotice)          
            {          
                ModelMessage listMsg = new ModelMessage(); 
                if (ModelState.IsValid) 
                { 
                    var result = im_emailnoticeService.AddEntity(m_emailnotice); 
                    listMsg=new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败"); 
                } 
                return Json(listMsg); 
            }  


         [AccountAuth(Roles = "M_EmailNotice_Edit")] 
         public ActionResult Edit(Guid? primarykey) 
         { 
             M_EmailNotice m_emailnotice = im_emailnoticeService.LoadEntities(x => x.EN_ID == primarykey).FirstOrDefault(); 
             if ( m_emailnotice == null) 
             { 
                 return HttpNotFound(); 
             } 
             return View( m_emailnotice); 
         } 
          
         [HttpPost] 
         [AccountAuth(Roles = "M_EmailNotice_Edit")] 
         public ActionResult Edit(M_EmailNotice formm_emailnotice) 
         { 
             ModelMessage listMsg = new ModelMessage(); 
             if (ModelState.IsValid) 
             { 
                 try 
                 { 
                     M_EmailNotice m_emailnotice = im_emailnoticeService.LoadEntities(x => x.EN_ID == formm_emailnotice.EN_ID).First();
 m_emailnotice.EN_ID= formm_emailnotice.EN_ID;      
 m_emailnotice.Projectd= formm_emailnotice.Projectd;      
 m_emailnotice.ProjectNumber= formm_emailnotice.ProjectNumber;      
 m_emailnotice.CreateEmailStatus= formm_emailnotice.CreateEmailStatus;      
 m_emailnotice.CreateEmailDateTime= formm_emailnotice.CreateEmailDateTime;      
 m_emailnotice.CreateEmailUserId= formm_emailnotice.CreateEmailUserId;      
 m_emailnotice.ES_Receiver= formm_emailnotice.ES_Receiver;      
 m_emailnotice.ES_CC= formm_emailnotice.ES_CC;      
 m_emailnotice.ES_Title= formm_emailnotice.ES_Title;      
 m_emailnotice.ES_Content= formm_emailnotice.ES_Content;      
 m_emailnotice.EN_NoticeStatus= formm_emailnotice.EN_NoticeStatus;      
 m_emailnotice.EN_CreateTime= formm_emailnotice.EN_CreateTime;      
 m_emailnotice.EN_ImpoertUserId= formm_emailnotice.EN_ImpoertUserId;      
                     bool result = im_emailnoticeService.UpdateEntity(m_emailnotice); 
                     listMsg=new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"); 
                 } 
                 catch (Exception e) 
                 { 
                     listMsg=new ModelMessage("ChangeResult", "m002", "保存失败"); 
                 } 
             } 
             return Json(listMsg); 
          
         } 
          
         [HttpPost] 
         [AccountAuth(Roles = "M_EmailNotice_Edit")] 
         public ActionResult ActivateSwich(string primarykeys, string activate = "N") 
         { 
             ModelMessage modelMessage = new ModelMessage(); 
          
             if (OperatorAvailable(primarykeys, activate)) 
             { 
                 modelMessage.Code = "m001"; 
                 modelMessage.Message = "修改成功"; 
             } 
             else 
             { 
                 modelMessage.Code = "m002"; 
             } 
             return Json(modelMessage, JsonRequestBehavior.AllowGet); 
         } 
          
         private bool OperatorAvailable(string primarykeys, string available) 
         { 
             List<Guid> listM_EmailNoticeGUID = primarykeys.Split(',').ToListGuid(); 
             List<M_EmailNotice> listM_EmailNotice = im_emailnoticeService.LoadEntities(x => listM_EmailNoticeGUID.Contains(x.EN_ID)).ToList(); 
             bool updaterow = im_emailnoticeService.DeleteListEntity(listM_EmailNotice); 
             if (updaterow ) 
             { 
                 return false; 
             } 
             return true; 
         } 
                                                     
                                                     
         public ActionResult Export()                
          {                                           
                 ServertLayuiTable<M_EmailNotice> servertBootStrapTable = new ServertLayuiTable<M_EmailNotice>();                                     
                  GetSearchData(ref servertBootStrapTable, false);                                   
                  List<ExcelColumn> columnInfo = new List<ExcelColumn>();                                   
                                                     
                                                     
             columnInfo.Add(new ExcelColumn("EN_ID", "EN_ID"));                                        
             columnInfo.Add(new ExcelColumn("Projectd", "Projectd"));                                        
             columnInfo.Add(new ExcelColumn("ProjectNumber", "ProjectNumber"));                                        
             columnInfo.Add(new ExcelColumn("CreateEmailStatus", "CreateEmailStatus"));                                        
             columnInfo.Add(new ExcelColumn("CreateEmailDateTime", "CreateEmailDateTime"));                                        
             columnInfo.Add(new ExcelColumn("CreateEmailUserId", "CreateEmailUserId"));                                        
             columnInfo.Add(new ExcelColumn("ES_Receiver", "ES_Receiver"));                                        
             columnInfo.Add(new ExcelColumn("ES_CC", "ES_CC"));                                        
             columnInfo.Add(new ExcelColumn("ES_Title", "ES_Title"));                                        
             columnInfo.Add(new ExcelColumn("ES_Content", "ES_Content"));                                        
             columnInfo.Add(new ExcelColumn("EN_NoticeStatus", "EN_NoticeStatus"));                                        
             columnInfo.Add(new ExcelColumn("EN_CreateTime", "EN_CreateTime"));                                        
             columnInfo.Add(new ExcelColumn("EN_ImpoertUserId", "EN_ImpoertUserId"));                                        
               byte[] btyBytes = null;                                        
                var filename = Server.MapPath("/") + @"DownloadDocument\" + System.Guid.NewGuid().ToString() + ".xlsx";     
                NPIOExportExcel.ExportExcelTest<M_EmailNotice>(servertBootStrapTable.data, filename, ref btyBytes, columnInfo, servertBootStrapTable.data.Count);                                      
                return File(filename, "text/comma-separated-values", "导出" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");                                      
                                                     
            }                                         
                                                     
            public ActionResult UploadFile(FormCollection form)                                          
            {                                         
                  string msg = string.Empty;                                   
                  if (Request.Files.Count == 0)                                   
                  {                                   
                        msg = "请选择文件";                              
                         return Json(msg);                             
                   }                                  
                    var file = Request.Files[0];                                  
                    if (file.ContentLength == 0)                                 
                    {                                 
                          msg = "请选择文件";                            
                           return Json(msg);                           
                    }                                 
                    file = Request.Files[0];                                 
                                                     
                   string virtualPath = Server.MapPath("/") + ("Attachment\\");                                          
                    Directory.CreateDirectory(virtualPath);                                  
                   string filename = System.Guid.NewGuid().ToString();//取得文件名字                                   
                  string path = virtualPath + filename;//获取存储的目标地址                                   
                  if (file.FileName.EndsWith(".xls"))                                   
                  {                                   
                      path += ".xls";                                
                   }                                  
                   else if (file.FileName.EndsWith(".xlsx"))                                  
                   {                                  
                        path += ".xlsx";                             
                    }                                 
                   else                                  
                   {                                  
                        msg = "只能上传Excel格式";                              
                        return Json(msg);                              
                   }                                  
                   file.SaveAs(path);                                  
                                                     
                    var result = M_EmailNoticeService.ReadExcel(path, CurrentUserInfo.User_Id,ref msg);                                  
                      msg = result ? "成功": "失败, "+ msg;                                
                       return Json(new { success = result, Message = msg });                               
                      }                               
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
                                                     
        }                                            
    }                                                
                                                     

